#!/bin/bash
#pTime
#Copyright 2014
#Sigmund Berglund - Valåmoen, Norway.

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------

VERSION=0.1

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"
export APPDIR="$APPDIR"
export WORKDIR=$HOME/.jwm/tmp
[ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"

#close app if already open
TMP="`ps -eo pid,command`"
if [ "`echo "$TMP" | grep -F 'JWM_time'`" ]; then
	for I in `echo "$TMP" | grep -F 'JWM_time' | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
	exit 0
fi

#set extended gtk-theme
echo 'style "mono" {
	font_name="Mono bold 12"
	base[NORMAL]="#111"
	text[NORMAL]="#B4CAB6" }
widget "*JWM_Calendar" style "mono"' > $WORKDIR/gtkrc_mono
export GTK2_RC_FILES=$WORKDIR/gtkrc_mono:/root/.gtkrc-2.0

#Y placement
TMP=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f2 -d 'x' | cut -f1 -d '+'`
HEIGHT=$((TMP-27)) #minus main-tray
[ ! "$HEIGHT" ] && HEIGHT=571 #in case
#X placement
TMP=`xwininfo -root | grep -m 1 '\geometry' | cut -f4 -d ' ' | cut -f1 -d 'x'`
X=$((TMP-250)) #minus main-tray
[ ! "$X" ] && X=1 #in case
#Y placement
[ "`cut -d' ' -f2 <<< $(getcurpos)`" -lt 100 ] && Y=27 || Y=1
#GMT
export MYGMT="`readlink /etc/localtime | cut -d/ -f6 | sed -e 's,^GMT-,GMTx,' -e 's,^GMT+,GMT-,' -e 's,^GMTx,GMT+,'`"

get_cal (){
	echo > $WORKDIR/cal
	cal $1 $2 >> $WORKDIR/cal
	echo $1 > $WORKDIR/cal_month
	echo $2 > $WORKDIR/cal_year
}

prev_month (){
	TMP_MONTH1="$(<$WORKDIR/cal_month)"
	if [ $TMP_MONTH1 = 1 ]; then
		TMP_YEAR1="$(<$WORKDIR/cal_year)"
		TMP_YEAR=$((${TMP_YEAR1}-1))
		TMP_MONTH=12
	else
		TMP_MONTH=$((${TMP_MONTH1}-1))
		TMP_YEAR="$(<$WORKDIR/cal_year)"
	fi
	get_cal $TMP_MONTH $TMP_YEAR
}

next_month (){
	TMP_MONTH1="$(<$WORKDIR/cal_month)"
	if [ $TMP_MONTH1 = 12 ]; then
		TMP_YEAR1="$(<$WORKDIR/cal_year)"
		TMP_YEAR=$((${TMP_YEAR1}+1))
		TMP_MONTH=1
	else
		TMP_MONTH=$((${TMP_MONTH1}+1))
		TMP_YEAR="$(<$WORKDIR/cal_year)"
	fi
	get_cal $TMP_MONTH $TMP_YEAR
}

export -f get_cal prev_month next_month

TMP=`date +"%m"`
TMP_MONTH=${TMP#0}
TMP=`date +"%Y"`
TMP_YEAR=${TMP#0}
get_cal $TMP_MONTH $TMP_YEAR

#run app
S='
<window title="JWM_time" width-request="250" height-request="'$HEIGHT'" decorated="false">
<vbox spacing="0" space-expand="true" space-fill="true">
  <vbox space-expand="false" space-fill="false">
    <hbox space-expand="true" space-fill="true">
      <button relief="2" space-expand="false" space-fill="false">
        '"`/usr/lib/gtkdialog/xml_button-icon close`"'
        <action>EXIT:exit</action>
      </button>
    </hbox>
  </vbox>
  <timer visible="false" milliseconds="true" interval="200">
    <action>/usr/lib/gtkdialog/svg_analogclock 220 '$MYGMT' > '$WORKDIR'/analogclock.svg</action>
    <action>refresh:PIX_ANALOG_CLOCK</action>
   </timer>
   <pixmap file-monitor="true" auto-refresh="true" space-expand="false" space-fill="false">
    <variable>PIX_ANALOG_CLOCK</variable>
    <input file>'$WORKDIR'/analogclock.svg</input>
  </pixmap>
  <vbox margin="10" space-expand="false" space-fill="false">
    <hbox space-expand="false" space-fill="false">
      <hbox space-expand="true" space-fill="true">
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon arrow_mini_left`"'
          <action>prev_month</action>
          <action>refresh:CAL</action>
        </button>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <text space-expand="false" space-fill="false"><label>"'$(date "+%A %d. %b")'"</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon arrow_mini_right`"'
          <action>next_month</action>
          <action>refresh:CAL</action>
        </button>
      </hbox>
    </hbox>
    <hbox height-request="180" space-expand="false" space-fill="false">
      <vbox space-expand="true" space-fill="true">
        <edit name="JWM_Calendar" editable="false" left_margin="10" vscrollbar-policy="2" hscrollbar-policy="2" space-expand="false" space-fill="false">
          <variable>CAL</variable>
          <input file>'$WORKDIR'/cal</input>
        </edit>
      </vbox>
    </hbox>
  </vbox>
  <text space-expand="true" space-fill="true"><label>""</label></text>
  <vbox space-expand="false" space-fill="false">'

    [ "`which defaultcalendar`" ] && S=$S'
    <hbox space-expand="true" space-fill="true">
      <text xalign="0.98" space-expand="true" space-fill="true"><label>'$(gettext 'Time planner')'</label></text>
      <button space-expand="false" space-fill="false">
        '"`/usr/lib/gtkdialog/xml_button-icon calendar`"'
        <action>defaultcalendar &</action>
      </button>
    </hbox>'

    S=$S'<hbox space-expand="true" space-fill="true">
      <text xalign="0.98" space-expand="true" space-fill="true"><label>'$(gettext 'Set time and date')'</label></text>
      <button space-expand="false" space-fill="false">
        '"`/usr/lib/gtkdialog/xml_button-icon date_time`"'
        <action>set-time-for-puppy &</action>
      </button>
    </hbox>
    <hbox space-expand="true" space-fill="true">
      <text xalign="0.98" space-expand="true" space-fill="true"><label>'$(gettext 'Clock format (tray)')'</label></text>
      <button space-expand="false" space-fill="false">
        '"`/usr/lib/gtkdialog/xml_button-icon preferences`"'
        <action>/usr/local/jwm_config/tray -clock &</action>
      </button>
    </hbox>
      <vbox>
        <hbox space-expand="true" space-fill="true">
          <text xalign="0.98" space-expand="true" space-fill="true"><label>'$(gettext 'Set timezone')' ('$MYGMT')</label></text>
          <button space-expand="false" space-fill="false">
            '"`/usr/lib/gtkdialog/xml_button-icon country_timezone`"'
            <action>timezone-set &</action>
          </button>
        </hbox>

        <hbox space-expand="true" space-fill="true">
          <text xalign="0.98" space-expand="true" space-fill="true"><label>'$(gettext 'Syncronize time with server')'</label></text>
          <button space-expand="false" space-fill="false">
            '"`/usr/lib/gtkdialog/xml_button-icon clock_countdown.svg`"'
            <action>psync &</action>
          </button>
        </hbox>'

        [ -s /usr/sbin/pclock ] && S=$S'
        <hbox space-expand="true" space-fill="true">
          <text xalign="0.98" space-expand="true" space-fill="true"><label>pClock - '$(gettext 'Time tools')'</label></text>
          <button space-expand="false" space-fill="false">
            '"`/usr/lib/gtkdialog/xml_button-icon clock_alarm`"'
            <action>pclock &</action>
          </button>
        </hbox>'
        [ -s /usr/local/pschedule/pschedule ] && S=$S'
        <hbox space-expand="true" space-fill="true">
          <text xalign="0.98" space-expand="true" space-fill="true"><label>'$(gettext 'Task scheduler')'</label></text>
          <button space-expand="false" space-fill="false">
            '"`/usr/lib/gtkdialog/xml_button-icon clock_interval`"'
            <action>pschedule &</action>
          </button>
        </hbox>'
      S=$S'</vbox>
      <label>'$(gettext 'More timing')'</label>
  </vbox>
  <text space-expand="false" space-fill="false"><label>""</label></text>
</vbox>
<action signal="button-release-event">EXIT:exit</action>
</window>'
export JWM_time="$S"
gtkdialog -p JWM_time -G +"$X"+"$Y"


